package com.q.tqb.activity.mine;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.activity.mine.wallet.WalletAddCardActivity;
import com.q.tqb.adapter.CardAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.MineCardInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.MD5Util;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.refresh.GRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class MineCardActivity extends BaseActivity {


    @BindView(R.id.rv_card)
    GRecyclerView mRvCard;
    @BindView(R.id.bt_add_card)
    Button mBtAddCard;

    private ArrayList<MineCardInfo> mMineCardInfos;
    private CardAdapter mCardAdapter;
    private int uid;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mine_card;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle("我的银行卡");
        uid = (int) SPUtil.get(this, "uid", 0);
        mMineCardInfos = new ArrayList<>();

        mRvCard.setLoadingMoreEnabled(false);
        mRvCard.setLoadingListener(new GRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                getListData();
            }

            @Override
            public void onLoadMore() {
            }
        });

        mCardAdapter = new CardAdapter(MineCardActivity.this, mMineCardInfos);
        mCardAdapter.setOnItemClickListener(new CardAdapter.OnItemClickListener() {
            @Override
            public void onClick(final int position) {
                final AlertDialog.Builder normalDialog = new AlertDialog.Builder(MineCardActivity.this);
                normalDialog.setTitle("温馨提示");
                normalDialog.setMessage("是否确认删除银行卡?");
                normalDialog.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                delData(mMineCardInfos.get(position).getId());
                                dialog.dismiss();
                            }
                        });
                normalDialog.setNegativeButton("关闭",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                normalDialog.show();

            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MineCardActivity.this);
        mRvCard.setLayoutManager(linearLayoutManager);
        mRvCard.setAdapter(mCardAdapter);
        mRvCard.setRefresh();


    }


    @OnClick({R.id.iv_back, R.id.bt_add_card})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_add_card:
                Intent intent = new Intent(MineCardActivity.this, WalletAddCardActivity.class);
                startActivityForResult(intent, 50);
                break;
            default:
                break;
        }
    }

    private void getListData() {
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid + "");
        map.put("token", StringUtil.getToken(uid));
        OkHttpUtil.getInstance().post(MineCardActivity.this, AppConst.URL_GET_BANK, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                ArrayList<MineCardInfo> getRedPersonDatas = gson.fromJson(element, new TypeToken<ArrayList<MineCardInfo>>() {
                }.getType());
                mRvCard.refreshComplete();
                mMineCardInfos.clear();
                mMineCardInfos.addAll(getRedPersonDatas);
                mCardAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                mRvCard.refreshComplete();
                showToast(errorMsg);
            }
        });
    }

    private void delData(int id) {
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid + "");
        map.put("id", id + "");
        map.put("token", StringUtil.getToken(uid));
        OkHttpUtil.getInstance().post(MineCardActivity.this, AppConst.URL_DEL_BANK, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                showToast("删除成功");
                getListData();


            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {

                showToast(errorMsg);
            }
        });
    }
}
